<?php
/**
 * Maintenance types
 * 
 */

use Common\Competitor;

$id = Xview::instance()->val('id');
try {
	$object = new Competitor($id);
}
catch(Fox\Object\Exception\NotExist $e )
{
	
}

if($object->exists())
{
	$title = Msg(Fox::MSGCL,59,$object->label());
	$button = "update";
}
else 
{
	$title = Msg(Fox::MSGCL,58);
	$button = "create";
}

$data = $object->getData();

$screen = new Fox\Html\Screen("pEdit",$button,null,true, "Refresh(\'?mod=maintenance&act=competitors\')");
$block = new Fox\Html\Block("bEdit");
$block->add(new Fox\Html\Input(array("competitor","name"),true));
$block->add(new Fox\Html\Input("id",false,$id))->hidden();
$block->initialize($data);
$screen->add($block);

if($screen->submitted() || isset($_POST['id']))
{
	$fields = $screen->get();
	try {
		
		$object->setFields($fields);
		if($object->exists())
		{
			$object->prepareUpdate($object::CHANGE_DEFAULT);
			$object->update();
			echo Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,$object->label()));
		}
		else
		{
			// Check duplicates
			$object->create();
			echo Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,$object->label()));
		}
		exit;
	}
	catch(Fox\Object\Exception\Save $e)
	{
		Fox\Core\Errors::pushMessage($e);
	}
	catch(Exception $e)
	{
		
		Fox\Message::Error($e->getMessage());
	}
}
$popup = new Fox\Popup("pEdit",$title,$screen,null,null,500);
echo $popup->Display();